package com.ojoin.trade.common.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ojoin.trade.common.autoconfig.base.ChangeableConfig;
import com.ojoin.trade.common.utils.JedisUtils;
import com.ojoin.trade.common.web.domain.BaseEntity;
import com.ojoin.trade.common.web.domain.Dict;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.service.DictService;

@Controller
@RequestMapping(value = "sys/dict")
public class DictController extends SimpleBaseController {

	@Autowired
	private DictService dictService;
	
	
	@ModelAttribute("dict")
	public Dict get(@RequestParam(required=false) Integer id) {
		if (null!=id){
			return  dictService.queryById(id);
		}else{
			return new Dict();
		}
	}
	 
	
	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value ="list")
	@ResponseBody
	public Result list(Dict dict, Pageable page) {
        return new Result(Result.Status.OK,dictService.queryList(dict, page));
	}

	
	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = "typeList")
	@ResponseBody
	public Result typeList() {
        return new Result(Result.Status.OK,dictService.findTypeList());
	}
 

	@RequiresPermissions("sys:dict:edit")
	@ResponseBody
	@RequestMapping(value = "save")//@Valid 
	public Result save(Dict dict) {
		dictService.save(dict);
		
		JedisUtils.publish("online.config",dict.getLabel());
				
		return new Result(Result.Status.OK, "保存字典'" + dict.getLabel() + "'成功");
	}
	
	@RequiresPermissions("sys:dict:edit")
	@ResponseBody
	@RequestMapping(value = "delete")
	public Result delete(Dict dict) {
		dict.setDelFlag(BaseEntity.DEL_FLAG_DELETE);
		dictService.delete(dict);
		return new Result(Result.Status.OK, "删除字典成功");
	}
	
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public Result treeData(@RequestParam(required=false) String type, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		Dict dict = new Dict();
		dict.setType(type);
		List<Dict> list = dictService.queryList(dict);
		for (int i=0; i<list.size(); i++){
			Dict e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getParentId());
			map.put("name", StringUtils.replace(e.getLabel(), " ", ""));
			mapList.add(map);
		}
		return new Result(Result.Status.OK, mapList);
	}
	
	@ResponseBody
	@RequestMapping(value = "listData")
	public List listData(@RequestParam(required=false) String type) {
		Dict dict = new Dict();
		dict.setType(type);
		return dictService.queryList(dict);
	}

}
